package od_2024E;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test01_zice01 {
    static DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm");

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 读取注册app数
        int count = Integer.parseInt(scanner.nextLine());
        // 收集所有的app
        List<AppInfo> appList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String[] input = scanner.nextLine().split(" ");
            String appName = input[0];
            int priority = Integer.parseInt(input[1]);
            LocalTime startTime = LocalTime.parse(input[2], pattern);
            LocalTime endTime = LocalTime.parse(input[3], pattern);
            // 过滤掉不合法的app
            if (startTime.compareTo(endTime) >= 0) {
                continue;
            }

            boolean canRegister = true;
            List<AppInfo> toRemove = new ArrayList<>();

            for (AppInfo existingApp : appList) {
                if (startTime.compareTo(existingApp.endTime) < 0 && existingApp.startTime.compareTo(endTime) < 0) {
                    if (priority > existingApp.priority) {
                        toRemove.add(existingApp);
                    } else {
                        canRegister = false;
                        break;
                    }
                }
            }

            if (canRegister) {
                appList.removeAll(toRemove);
                appList.add(new AppInfo(appName, priority, startTime, endTime));
            }
        }

        String queryTimeStr = scanner.nextLine();
        LocalTime queryTime = LocalTime.parse(queryTimeStr);
        String result = "NA";

        for (AppInfo app : appList) {
            if (app.startTime.compareTo(queryTime) <= 0 && queryTime.compareTo(app.endTime) < 0) {
                result = app.name;
                break;
            }
        }
        System.out.println(result);
        scanner.close();
    }

    // 定义App类，用于存储App的相关信息
    static class AppInfo {
        String name; // App名称
        int priority; // App优先级
        LocalTime startTime; // App允许使用的起始时间
        LocalTime endTime; // App允许使用的结束时间

        // App类的构造函数，用于创建App对象
        public AppInfo(String name, int priority, LocalTime startTime, LocalTime endTime) {
            this.name = name;
            this.priority = priority;
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }
}
